package com.cdd.servicebankcard.service.impl;

import com.cdd.servicebankcard.dao.AccountInfoMapper;
import com.cdd.servicebankcard.model.AccountInfo;
import com.cdd.servicebankcard.service.AccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description TODO
 * @Author: cdd
 * @Date: 2020/7/9 15:31
 * @Version: 1.0
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    AccountInfoMapper accountInfoMapper;

    @Override
    public AccountInfo getById(Long id) {
        return accountInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional // 本地事务
    public int updateAccountById(Integer amount) {
        AccountInfo accountInfo = getById(1L);
        accountInfo.setBalance(accountInfo.getBalance() + amount);
        int rs = accountInfoMapper.updateByPrimaryKeySelective(accountInfo);
        Assert.state(rs == 1, "更新银行卡账户影响的记录行数不为1");
        return rs;
    }


}
